define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
        method = app.method;

    var _ = require("underscore")
    var Emotion = function(element, textarea) {
        this.$element = element;
        var defaults = {
            x: 0,
            y: 0,
        };
        this.url = www_head + "/assets/images/face/{0}.gif";
        this.faceList = {
            0: "微笑",
            1: "撇嘴",
            2: "色",
            3: "发呆",
            4: "得意",
            5: "流泪",
            6: "害羞",
            7: "闭嘴",
            8: "睡",
            9: "大哭",
            10: "尴尬",
            11: "发怒",
            12: "调皮",
            13: "呲牙",
            14: "惊讶",
            15: "难过",
            16: "酷",
            17: "冷汗",
            18: "抓狂",
            19: "吐",
            20: "偷笑",
            21: "可爱",
            22: "白眼",
            23: "傲慢",
            24: "饥饿",
            25: "困",
            26: "惊恐",
            27: "流汗",
            28: "憨笑",
            29: "大兵",
            30: "奋斗",
            31: "咒骂",
            32: "疑问",
            33: "嘘",
            34: "晕",
            35: "折磨",
            36: "衰",
            37: "骷髅",
            38: "敲打",
            39: "再见",
            40: "擦汗",
            41: "抠鼻",
            42: "鼓掌",
            43: "糗大了",
            44: "坏笑",
            45: "左哼哼",
            46: "右哼哼",
            47: "哈欠",
            48: "鄙视",
            49: "委屈",
            50: "快哭了",
            51: "阴险",
            52: "亲亲",
            53: "吓",
            54: "可怜",
            55: "菜刀",
            56: "西瓜",
            57: "啤酒",
            58: "篮球",
            59: "乒乓",
            60: "咖啡",
            61: "饭",
            62: "猪头",
            63: "玫瑰",
            64: "凋谢",
            65: "示爱",
            66: "爱心",
            67: "心碎",
            68: "蛋糕",
            69: "闪电",
            70: "炸弹",
            71: "刀",
            72: "足球",
            73: "瓢虫",
            74: "便便",
            75: "月亮",
            76: "太阳",
            77: "礼物",
            78: "拥抱",
            79: "强",
            80: "弱",
            81: "握手",
            82: "胜利",
            83: "抱拳",
            84: "勾引",
            85: "拳头",
            86: "差劲",
            87: "爱你",
            88: "NO",
            89: "OK",
            90: "爱情",
            91: "飞吻",
            92: "跳跳",
            93: "发抖",
            94: "怄火",
            95: "转圈",
            96: "磕头",
            97: "回头",
            98: "跳绳",
            99: "挥手",
            100: "激动",
            101: "街舞",
            102: "献吻",
            103: "左太极",
            104: "右太极"
        };
        $("#qywechat_qqface").length && $("#qywechat_qqface").remove();
        this.$body = $('<div id="qywechat_qqface" class="emoji_wrap hide"><div class="emoji_inner"></div></div>');
        this.isInit = false;
        this.$textarea = textarea;
        this.dfaceList = _.invert(this.faceList)
        this.$faceArea = $(".emoji_inner", this.$body);
        var _self = this;
        if (element) {
             _self.init();
              this.$element.on("click", function(e) {
            !_self.isInit ? _self.show() : _self.hide()

        }); 
        }; 

    };

    Emotion.prototype.init = function() {
        var _self = this;
        var item_template = template.compile('<a title="{{title}}" class="{{cssClass}}" href="javascript:;"></a>');
        $.each(this.faceList, function(index, val) {
            var index_int = (parseInt(index) + 1);
            var rclassName = index_int % 15 ? '' : 'borderRightNone';
            var bclassName = index_int < 90 ? '' : ' borderBottomNone';
            var className = rclassName + bclassName
            _self.$faceArea.append(item_template({
                title: val,
                cssClass: className

            }))
        });
        $(document).on("click", function(e) {
            if (_self.isInit) {
                var $el = $(e.target);
                if (!$el.hasClass('js_emoticon')) {
                    $el.closest("#qywechat_qqface").length || _self.hide()
                };
            };
        })
        this.$element.closest('.input_edition_wrap').append(this.$body);
        this.$body.on("click", '.emoji_inner a', function(e) {
            _self.writeFace(e);
            _self.hide();
        })
    }
    Emotion.prototype.writeFace = function(e) {
        var $item = $(e.target),
            face = "[" + $item.attr("title") + "]";
        this.$textarea.insertAtCaret(face);
        this.$textarea.trigger('keyup');
    }
    Emotion.prototype.show = function() {
        this.$body.removeClass('hide');
        this.isInit = true;
    }
    Emotion.prototype.hide = function() {
        this.$body.addClass('hide')
        this.isInit = false;
    }
    Emotion.prototype.translation = function(html) {
        if (!html) return "";
        var ahtml = html.replace("[", "").replace("]", "");
        // var b = parseInt(this.dfaceList[ahtml]) + 100; //返回表情副本
        // return b ? '<img src="http://cache.soso.com/img/img/e' + b + '.gif" style="vertical-align:bottom">' : "";
        var b = parseInt(this.dfaceList[ahtml]) + 100; // 返回静态服务器资源
        return b ? '<img src="{0}" style="vertical-align:bottom">'.format(this.url.format('e' + b)) : ""

    }
    Emotion.prototype.htmltranslation = function(html) {
        var _self = this;
        if (!html) {
            return '';
        };
        html = _.escape(html);
        html = $.trim(html);
        var HTML_ESCAPES = {
                "\n": "<br/>",
                " ": " "
            },
            REG = new RegExp("[" + _.keys(HTML_ESCAPES).join("") + "]", "g");
        return html = html.replace(REG, function(t) {
            return HTML_ESCAPES[t];
        }), html = html.replace(new RegExp("\\[[^\\[\\]]*\\]", "g"), function(t) {
            var c = t.replace("[", "").replace("]", "");
            return _self.dfaceList[c] ? _self.translation(c) : html;
        })

    }
  
    return Emotion;

})
